﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoStore.Business.Components.Interfaces;
using VideoStore.Business.Entities;
using System.Transactions;
using VideoStore.Common;
using Microsoft.Practices.ServiceLocation;
using MessageBus.Interfaces;

namespace VideoStore.Business.Components
{
    public class OrderProvider : IOrderProvider
    {
        private IEmailProvider EmailProvider
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IEmailProvider>();
            }
        }

        public Order GetOrderById(int pId)
        {
            using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer())
            {
                lContainer.ContextOptions.LazyLoadingEnabled = false;
                return (from order in lContainer.Orders.Include("OrderItems.Media.Stocks").Include("Customer") where order.Id == pId select order).FirstOrDefault();
            }
        }


        public void CancelOrder(Order pOrder)
        {
            try
            {
                using (TransactionScope lScope = new TransactionScope())
                using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer())
                {
                    lContainer.Orders.Attach(pOrder);
                    lContainer.ObjectStateManager.ChangeObjectState(pOrder, System.Data.EntityState.Modified);
                    pOrder.UndoStockLevelChanges();
                    lContainer.ApplyChanges<Order>("Orders", pOrder);
                    lContainer.SaveChanges();
                    lScope.Complete();
                }
            }
            catch (Exception lException)
            {
                System.Diagnostics.Debug.WriteLine(lException.Message);
                throw;
            }
        }

        public void SubmitOrder(Entities.Order pOrder)
        {
            String lMessage = String.Format("Your order has been submitted successfully");
            try
            {
                ServiceLocator.Current.GetInstance<IPublisherService>().Publish(
                    CommandFactory.Instance.GetSubmitOrderCommand(pOrder)
                );
            }
            catch (Exception lException)
            {
                lMessage = lException.Message;
                //throw;
            }
            finally
            {
                EmailProvider.SendMessage(pOrder.Customer.Email, lMessage);
            }
        }

        public void ConfirmOrder(int pId, Boolean outcome)
        {
            String lMessage = String.Format("Your order has been fulfilled successfully");
            Order pOrder = GetOrderById(pId);
            if (outcome)
            {
                EmailProvider.SendMessage(pOrder.Customer.Email, lMessage);
            }
            else
            {
                lMessage = String.Format("Your order is not fulfilled due to insufficient funds in bank account");
                CancelOrder(pOrder);
                EmailProvider.SendMessage(pOrder.Customer.Email, lMessage);
            }
        }
    }
}
